package com.ynr.voucher.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ynr.dao.IBankDao;
import com.ynr.dao.ICompanyDao;
import com.ynr.voucher.service.IVoucherBankService;
import com.ynr.voucher.service.IVoucherService;
import com.ynr.voucher.utils.ExcelReader;

@Service
public class VoucherBankServiceImpl implements IVoucherBankService {

	@Autowired
	private IBankDao bankDao;
	
	@Autowired
	private ICompanyDao companyDao;
	
	@Autowired
	private IVoucherService voucherService;
	
	@Override
	public void processBankStatement(String filePath, int companyId, String bankType, String billDate) {
		Map<String, Object> bankTitle = bankDao.getBankTitle(bankType);
		List<Map<String, String>> bankStatementContent = ExcelReader.parseBankStatement(filePath, bankTitle);
		Map<String, Object> bankStatementMap = new HashMap<>();
		bankStatementMap.put("company_id", companyId);
		bankStatementMap.put("bill_date", billDate);
		bankStatementMap.put("filepath", filePath);
		bankDao.addBankStatement(bankStatementMap);
		for(Map<String, String> item : bankStatementContent) {
			item.put("bsid", String.valueOf(bankStatementMap.get("id")));
		}
		bankDao.addBankStatementDetail(bankStatementContent);
		
		Map<String, Object> companyMap = companyDao.getCompanyById(companyId);
		voucherService.generateBankstatementVoucher(companyMap, 2, billDate, bankStatementContent);
	}

}
